package com.dididoctor.doctor.Activity.PatientDetial;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.SharedPreferencesUtils;
import com.dididoctor.doctor.WYY.SessionHelper;
import com.netease.nim.uikit.session.activity.P2PMessageActivity;
import com.netease.nim.uikit.session.constant.Extras;

import java.util.ArrayList;
import java.util.List;


/**
 * @version 2.0
 * @类描述： 首页MVP
 * @项目名称：douDouTech
 * @类名称： SplashActivity
 * @包名称： com.doudoukeji.service.Activity.mvp
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class PatientDetialTHActivity extends TabActivity implements View.OnClickListener {

    private TabHost tabHost;

    //标题
    private TextView mTvTitle;
    private ImageView mImgSearch, mImgScan;
    //返回按钮
    private ImageView mBtnBack;
    private MyBroadcastReceiver mMyBroadcastReceiver;
    private TextView tv_title1, tv_title2, tv_title3;
    private List<TextView> ltv = new ArrayList<TextView>();
    private final int[] normal = new int[]{R.id.tv_title1, R.id.tv_title2, R.id.tv_title3};
    private int[] bint = new int[]{R.color.main_color, R.color.sapi_background_color, R.color.white};
    //  isCallorpictury  1-即时咨询 2-电话咨询
    private String clientId, status, isSign, accId, isCallorpictury,istalk;
    private boolean ispatient = false;
    private int CurrentTab = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除titilebar使用的
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.patient_detail_tab_frg);

        clientId = getIntent().getStringExtra("clientId");
        SharedPreferencesUtils.setParam(PatientDetialTHActivity.this, "Medicaladvice_use_clientId", clientId);
        status = getIntent().getStringExtra("status");

        isSign = getIntent().getStringExtra("isSign");
        SharedPreferencesUtils.setParam(PatientDetialTHActivity.this, "Medicaladvice_use_isSign", isSign);
        accId = getIntent().getStringExtra("accId");
//        1-即时咨询 2-电话咨询
        isCallorpictury = getIntent().getStringExtra("isCallorpictury");
        SharedPreferencesUtils.setParam(PatientDetialTHActivity.this,"Medicaladvice_use_isCallorpictury",isCallorpictury);

        CurrentTab = getIntent().getIntExtra("CurrentTab", 1);
        //是否是从患者跳进来的
        ispatient = getIntent().getBooleanExtra("ispatient", false);

        istalk = getIntent().getStringExtra("istalk");

        //初始化控件
        init();
    }

    /**
     * 初始化控件
     */
    private void init() {
        /***********标题栏************/
        initTopBar();

        /***********底边栏************/
        initBottomBar();

//        /***********广播*****************/
//        initBroadcast();

        try {
            this.tabHost = getTabHost();
            Intent intent = new Intent();
            intent.putExtra(Extras.EXTRA_ACCOUNT, accId);
            intent.putExtra(Extras.EXTRA_CUSTOMIZATION, SessionHelper.getP2pCustomization(accId));
            intent.putExtra("rank", "1");
            //  isCallorpictury  1-即时咨询 2-电话咨询
            intent.putExtra("isCallorpictury", isCallorpictury);
            intent.setClass(this, P2PMessageActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            1-未处理 2-进行中 3-已完结 4-已过期 5-已退款
            //chatstatus = 0 不可以聊天  1可以聊天
            String chatstatus = "0";
            //如果是从患者跳进来的
            if (ispatient) {
                if ("0".equals(istalk)){
                    chatstatus = "0";
                }else{
                    chatstatus = "1";
                }

                if ("0".equals(isSign)) {
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "0")).setIndicator("TAB1"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "1")).setIndicator("TAB3"));
                } else if ("1".equals(isSign)) {
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, PersonalDataActivit.class).putExtra("clientId", clientId)).setIndicator("TAB1"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, CaseHistoryActivity.class).putExtra("clientId", clientId)).setIndicator("TAB3"));
                }
            } else {
//                1-未处理
                if ("1".equals(status)) {
                    chatstatus = "1";
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, PersonalDataActivit.class).putExtra("clientId", clientId)).setIndicator("TAB1"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, CaseHistoryActivity.class).putExtra("clientId", clientId)).setIndicator("TAB3"));
                } else if ("2".equals(status)) {
//                    2-进行中
                    chatstatus = "1";
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, PersonalDataActivit.class).putExtra("clientId", clientId)).setIndicator("TAB1"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, CaseHistoryActivity.class).putExtra("clientId", clientId)).setIndicator("TAB3"));
                } else if ("3".equals(status)) {
                    chatstatus = "0";
//                    3-已完结
                    if ("0".equals(isSign)) {
                        this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "0")).setIndicator("TAB1"));
                        this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                        this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "1")).setIndicator("TAB3"));
                    } else if ("1".equals(isSign)) {
                        this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, PersonalDataActivit.class).putExtra("clientId", clientId)).setIndicator("TAB1"));
                        this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                        this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, CaseHistoryActivity.class).putExtra("clientId", clientId)).setIndicator("TAB3"));
                    }

                } else if ("4".equals(status)) {
//                    4-已过期
                    chatstatus = "0";
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "0")).setIndicator("TAB1"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "1")).setIndicator("TAB3"));
                }else if ("4".equals(status)){
//                    5-已退款
                    chatstatus = "0";
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "0")).setIndicator("TAB1"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(intent.putExtra("chatstatus", chatstatus)).setIndicator("TAB2"));
                    this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, NoAuthorityActivity.class).putExtra("status", "1")).setIndicator("TAB3"));
                }
            }


            this.tabHost.setCurrentTab(CurrentTab);
            seleted(CurrentTab);
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e("EXCEPTION", ex.getMessage());
        }
    }

    /**
     * 初始化广播
     */
    private void initBroadcast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConstantValue.Main_CHANGE);
        mMyBroadcastReceiver = new MyBroadcastReceiver();
        //消息红点
        registerReceiver(mMyBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (mMyBroadcastReceiver !=null){
                unregisterReceiver(mMyBroadcastReceiver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化底边栏
     */
    private void initBottomBar() {
        findViewById(R.id.layout1).setOnClickListener(this);
        findViewById(R.id.layout2).setOnClickListener(this);
        findViewById(R.id.layout3).setOnClickListener(this);

        tv_title1 = (TextView) findViewById(R.id.tv_title1);
        tv_title2 = (TextView) findViewById(R.id.tv_title2);
        tv_title3 = (TextView) findViewById(R.id.tv_title3);
        ltv.add(tv_title1);
        ltv.add(tv_title2);
        ltv.add(tv_title3);
    }

    /**
     * 初始化标题栏
     */
    private void initTopBar() {

        // 设置标题
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("患者详情");
        // 隐藏右侧按钮
        mBtnBack = (ImageView) findViewById(R.id.btn_back);
        mBtnBack.setVisibility(View.GONE);
        //設置搜索按鈕
        mImgSearch = (ImageView) findViewById(R.id.img_search);
        mImgSearch.setBackgroundResource(R.drawable.icon_search);
        mImgSearch.setOnClickListener(this);
        mImgSearch.setVisibility(View.GONE);
        //設置扫一扫按鈕
        mImgScan = (ImageView) findViewById(R.id.img_title);
        mImgScan.setVisibility(View.GONE);
        mImgScan.setOnClickListener(this);

    }


    //设置状态选择效果
    private void seleted(int i) {
        for (int j = 0; j < ltv.size(); j++) {
            if (j == i) {
                ltv.get(j).setBackgroundColor(Color.parseColor("#fcb314"));
                ltv.get(j).setTextColor(Color.parseColor("#ffffff"));
                this.tabHost.setCurrentTab(i);
            } else {
                ltv.get(j).setBackgroundColor(Color.parseColor("#ffffff"));
                ltv.get(j).setTextColor(Color.parseColor("#fcb314"));
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout1:
                seleted(0);
                break;
            case R.id.layout2:
                seleted(1);
                break;
            case R.id.layout3:
                seleted(2);
                break;
        }
    }

    private class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ConstantValue.Main_CHANGE)) {
                String position = intent.getStringExtra("position");
                tabHost.setCurrentTab(Integer.parseInt(position));
                seleted(Integer.parseInt(position));
            }
        }
    }


}
